﻿using System;
using System.Collections.Generic;
using System.Data;
using Sistema.Util.Interfaces;
using MySql.Data.MySqlClient;


namespace Sistema.InnoDbDAO
{
    class BancoDados : IBancoDados
    {
        private IDbConnection _conexao;
        private Queue<String> _Fila = new Queue<string>();
        public System.Data.IDbConnection Conexao
        {
            get { return _conexao; }
            set { _conexao = value; }
        }
        
        public Queue<string> Fila
        {
            get { return this._Fila; }
            set { this._Fila = value; }
        }

        private static IBancoDados _Banco;
        public static IBancoDados Banco()
        {
            if(_Banco == null)
            {
                _Banco = new BancoDados();
            }
            return _Banco;
        }

        private BancoDados()
        {
            String connectiosString;
            connectiosString = String.Format("server={0};user id={1}; password={2}; database={3}; port={4}; pooling=false; Allow User Variables=true;",
                "localhost", "root", "", "nota_fiscal_eletronica", "3306");
            this.Conectar(connectiosString);
        }
        private BancoDados(String connectionString)
        {
            this.Conectar(connectionString);
        }

        public System.Data.IDbConnection Conectar(string ConnectionString)
        {
            IDbConnection conexao;

            conexao = new MySqlConnection(ConnectionString);
            try
            {
                conexao.Open();
            }
            catch (MySqlException erro)
            {

            }
            return conexao;
        }


        public System.Data.IDataReader Buscar(string comando)
        {
            IDataReader reader;
            IDbCommand sql = new MySqlCommand(comando);
            reader = null;
            try
            {
                sql.Connection = Conexao;
                reader = sql.ExecuteReader();
                sql.Dispose();
            }
            catch (MySqlException erro)
            {
     
            }
            finally
            {
                sql.Dispose();
                sql = null;
            }
            return reader;
        }

        public void Executar(string comando)
        {
            IDbCommand sql = new MySqlCommand();
            
            try
            {
                sql.Connection = Conexao;
                sql.CommandText = comando;
                sql.ExecuteNonQuery();
            }
            catch (MySqlException ex)
            {

            }
            finally
            {
                sql.Dispose();
                sql = null;
            }
        }

        public void Executar(IDbCommand comando)
        {
            try
            {
                comando.Connection = Conexao;
                comando.ExecuteNonQuery();
            }
            catch (MySqlException ex)
            {

            }
            finally
            {
                comando.Dispose();
                comando = null;
            }
        }

        public void ExecutarFila()
        {
            IDbCommand comandos = Conexao.CreateCommand();
            IDbTransaction transacao;
            transacao = Conexao.BeginTransaction();
            comandos.Connection = Conexao;
            comandos.Transaction = transacao;
            try
            {
                while (Fila.Count != 0)
                {
                    comandos.CommandText = Fila.Dequeue();
                    comandos.ExecuteNonQuery();
                }
                transacao.Commit();
            }
            catch (MySqlException ex)
            {
                try
                {
                    transacao.Rollback();
                }
                catch (Exception erro)
                {
                    String comandoEmExecucao = comandos.CommandText.ToString() + "; Rollback".ToString();
                }
            }
            finally
            {
                transacao.Dispose();
                comandos.Dispose();
                transacao = null;
                comandos = null;
            }
        }
    }
}
